//
//  HMStatusRetweetView.swift
//  Weibo22
//
//  Created by Apple on 16/12/4.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import YYText

//  转发微博视图
class HMStatusRetweetView: UIView {
    //  记录转发微博底部约束
    var retweetBottomContraint: Constraint?
    
    //  需要视图模型
    var statusViewModel: HMStatusViewModel? {
        didSet {
            //  绑定转发微博数据
//            contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            //  设置配图信息
            //  卸载上次顶部约束
            retweetBottomContraint?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  有配图,显示配图,更新约束
                
                //  显示配图
                pictureView.isHidden = false
                
                self.snp_updateConstraints(closure: { (make) in
                    retweetBottomContraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusItemMargin).constraint
                })
                
                //  设置配图信息
                pictureView.picUrls = picUrls
            } else {
                //   没有配图, 更新约束
                pictureView.isHidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    retweetBottomContraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusItemMargin).constraint
                })
                
            }
            
            
        }
    }
    
    //  MARK: -- 懒加载
    private lazy var contentLabel: YYLabel = {
        
//        let label = UILabel(fontSize: 14, textColor: UIColor.gray)
        let label = YYLabel()
        
        //创建一个文本行位置修改类
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 22;
        
        label.linePositionModifier = modifier;
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * StatusItemMargin
        //  多行显示
        label.numberOfLines = 0
        
        label.text = "案件时打开;房间爱死了可对方即可;了解爱上对方可"
        
        return label
        
    }()
    
    //  配图
    private lazy var pictureView: HMStatusPictureView = {
        let  pictureView = HMStatusPictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  设置背景色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(StatusItemMargin)
            make.leading.equalTo(self).offset(StatusItemMargin)
            make.width.equalTo(ScreenWidth - 2 * StatusItemMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusItemMargin)
            make.leading.equalTo(contentLabel)
            
        }
        
        //  关键约束 (当前视图的底部等于内容的底部约束加上一个间距)
        self.snp_makeConstraints { (make) in
            retweetBottomContraint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
        }
        
        //  设置高亮文字点击执行的闭包
        
        //  containerView: 容器视图-> YYLabel
        //  attributedStr        : YYLabel显示的富文本
        //  range       : 点击高亮文字的范围
        //  rect         : 点击高亮文字的区域
        contentLabel.highlightTapAction = { (containerView, attributedStr, range, rect) in
            let result = (attributedStr.string as NSString).substring(with: range)
            print(result)
            
            /// ....业务逻辑处理
        }
        
    }

}
